import requests
from bs4 import BeautifulSoup
import csv
from urllib.parse import urljoin
def get_html(url):
    head={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"}
    try:
        r = requests.get(url=url,headers=head)
        r.encoding = r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as e:
        print(e)


def parser(html):
    soup=BeautifulSoup(html,"lxml")
    out_list=[]
    for row in soup.select("#colR > div.sortlist > dl > dd > ul > li"):
        biaoti = row.select("a")[0].text.strip()
        wangzhi = "http://www.cqie.edu.cn/"
        wangzhi2 = row.select("a")[0].attrs['href']
        wz = urljoin(wangzhi,wangzhi2)
        row_data = [biaoti,wz]
        out_list.append(row_data)
    return out_list

def save_csv(item,path):
    with open(path,"a+",newline='',encoding="utf-8")as f:
        csv_write = csv.writer(f)
        csv_write.writerow(item)
if __name__ == '__main__':
    url="https://www.cqie.edu.cn/html/2/xydt/Index.shtml"
    html=get_html(url)
    out_list=parser(html)
    save_csv(out_list,"majingtao.csv")